package dao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import po.Activity;
import po.User;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import myUtils.Constants;
import po.Comment;
import po.Useractrelation;
/**
 *
 * @author HX
 */
public class ActivityDaoImpl extends HibernateDaoSupport implements ActivityDao{
    public void save(Activity activity) throws Exception{
        getHibernateTemplate().saveOrUpdate(activity);
    }
    public void deleteActivity(int activityId) throws Exception{
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public Activity getActivity(int activityId){
        return (Activity) this.getHibernateTemplate().get(Activity.class, activityId);
    }
    public List<Activity> getUserActivities(int userId){
        String queryString="Select a from Activity as a inner join a.useractrelations as r with r.user.id=" + userId;
        return (List<Activity>)this.getHibernateTemplate().find(queryString);
    }
    public List<Useractrelation> getUseractrelation(int activityId){
        String queryString="select uar from Useractrelation as uar where uar.activity.id=" + activityId;
        return (List<Useractrelation>)this.getHibernateTemplate().find(queryString);
    }

    public List<Activity> getOrgActivities(int orgId){
        String queryString="Select o.activities from Organization as o where o.id=" + orgId;
        return (List<Activity>)this.getHibernateTemplate().find(queryString);
    }
    public List getActors(int activityId){
        String queryString="Select uar.user.id from Useractrelation as uar where uar.activity.id=" + activityId;
        return (List)this.getHibernateTemplate().find(queryString);
    }
    public List getMembers(int orgId){
        String queryString="Select uor.user.id, uor.user.name from Userorgrelation as uor where uor.organization.id=" + orgId;
        return (List)this.getHibernateTemplate().find(queryString);
    }
    public boolean isActor(int activityId, int userId){
        String queryString="from Useractrelation as uar where uar.activity.id=" + activityId + "and uar.user.id=" + userId;
        return !getHibernateTemplate().find(queryString).isEmpty();
    }

    public void editActivityBehavior(int activityId, int userId, String behavior) {
        String queryString="select ua from Useractrelation as ua where ua.activity.id="+ activityId +" and ua.user.id=" + userId;
        Useractrelation uar = (Useractrelation)this.getHibernateTemplate().find(queryString).get(0);
        uar.setDescription(behavior);
        this.getHibernateTemplate().update(uar);
    }

    public void editActivityScore(int activityId, int userId, int score) {
        String queryString="select ua from Useractrelation as ua where ua.activity.id="+ activityId +" and ua.user.id=" + userId;
        Useractrelation uar = (Useractrelation)this.getHibernateTemplate().find(queryString).get(0);
        uar.setScore(score);
        this.getHibernateTemplate().update(uar);
    }

    public void joinActivity(Useractrelation useractrelation)
    {
        this.getHibernateTemplate().saveOrUpdate(useractrelation);
    }

    public void addUseractrelations(List<Useractrelation> useractrelations){
        this.getHibernateTemplate().saveOrUpdateAll(useractrelations);
    }

    public void askForLeave(int activityId, int userId){
        String queryString="select ua from Useractrelation as ua where ua.activity.id="+ activityId +" and ua.user.id=" + userId;
        Useractrelation uar = (Useractrelation)this.getHibernateTemplate().find(queryString).get(0);
        uar.setState(Constants.ASK_FOR_LEAVE);
        this.getHibernateTemplate().update(uar);
        
    }

    public List<Comment> getComments(int activityId){
        String queryString = "select c from Comment as c where c.activity.id=" + activityId;
        return (List<Comment>)this.getHibernateTemplate().find(queryString);
    }

    public void addComment(int activityId, int userId, String content){
        Comment c = new Comment();
        c.setActivity((Activity) this.getHibernateTemplate().get(Activity.class, activityId));
        c.setUser((User) this.getHibernateTemplate().get(User.class, userId));
        c.setContent(content);
        c.setTime(new Date());
        this.getHibernateTemplate().save(c);
    }

    public void deleteComment(int commentId){
        Object p = getHibernateTemplate().load(Comment.class, commentId);
        this.getHibernateTemplate().delete(p);
    }
}
